package com.wfuhui.modules.order.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.wfuhui.modules.order.entity.FinanceGroup;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wfuhui.modules.order.entity.OrderEntity;
import com.wfuhui.modules.order.entity.OrderGoods;
import com.wfuhui.modules.order.service.OrderService;
import com.wfuhui.modules.store.entity.StoreEntity;
import com.wfuhui.modules.store.service.StoreService;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

import com.wfuhui.common.utils.Query;
import com.wfuhui.common.utils.R;


/**
 * 订单
 * 
 * @author xiaotian
 * @email codemax@qq.com
 * @date 07-14 13:43:12
 */
@RestController
@RequestMapping("/order")
public class OrderController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private StoreService storeService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		if(params.get("statuses") != null && !"".equals(params.get("statuses"))) {
			params.put("statuses", params.get("statuses").toString().split(","));
		}
		//查询列表数据
		Query query = new Query(params);

		List<OrderEntity> orderList = orderService.queryList(query);
		int total = orderService.queryTotal(query);

		return R.ok().put("rows", orderList).put("total", total);

	}

	/**
	 * 列表
	 */
	@RequestMapping("/group")
	public R group(@RequestParam Map<String, Object> params) {
		//查询列表数据
		Query query = new Query(params);

		List<FinanceGroup> orderList = orderService.queryGroup(query);
		int total = storeService.queryTotal(query);

		return R.ok().put("rows", orderList).put("total", total);

	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{orderId}")
	public R info(@PathVariable("orderId") Integer orderId) {
		OrderEntity order = orderService.queryObject(orderId);

		return R.ok().put("order", order);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("order:save")
	public R save(@RequestBody OrderEntity order) {
		orderService.save(order);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("order:update")
	public R update(@RequestBody OrderEntity order) {
		orderService.update(order);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Integer[] orderIds) {
		orderService.deleteBatch(orderIds);

		return R.ok();
	}

	/**
	 * 发货
	 */
	@RequestMapping("/sendGoods/{orderId}")
	public R sendGoods(@PathVariable("orderId") Integer orderId, String courierNum) {
		OrderEntity order = new OrderEntity();
		order.setId(orderId);
		order.setOrderStatus(3);
		orderService.update(order);
		return R.ok();
	}

	@RequestMapping("/exportExcel")
	public void exportExcel(@RequestParam Map<String, Object> params, HttpServletResponse resp) throws IOException {
		List<OrderEntity> orderList = orderService.queryList(params);
		Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("订单列表", "订单"), OrderEntity.class, orderList);
		resp.setHeader("Content-disposition", "order.xls");
		resp.setContentType("application/vnd.ms-excel");
		resp.setHeader("Content-disposition", "attachment;filename=order.xls");
		resp.setHeader("Pragma", "No-cache");
		workbook.write(resp.getOutputStream());
	}

	@RequestMapping("/queryByStoreId")
	public R queryByStoreId(@RequestParam Map<String, Object> params){
		if(params.get("statuses") != null && !"".equals(params.get("statuses"))) {
			params.put("statuses", params.get("statuses").toString().split(","));
		}
		List<OrderEntity> orderList = orderService.queryList(params);
		return R.ok().put("orderList", orderList);
	}

	@RequestMapping("/queryStatistics")
	@RequiresPermissions("order:list")
	public R queryStatistics(@RequestParam Map<String, Object> params){
		params.put("statuses", new Integer[] {2, 3});
		
		List<OrderGoods> orderGoods = orderService.queryStatistics(params);
		BigDecimal totalAmount = orderService.queryTotalAmount(params);

		R reuslt = R.ok().put("orderGoods", orderGoods).put("totalAmount", totalAmount);
		if(params.get("storeId") != null){
			StoreEntity store = storeService.queryObject(Integer.parseInt(params.get("storeId").toString()));
			reuslt.put("store", store);
		}
		return reuslt;

	}

	@RequestMapping("/sendSms")
	public R sendSms(Integer storeId){
		StoreEntity store = storeService.queryObject(storeId);
		//SmsUtils.send(SmsUtils.NOTICE, store.getUser().getMobile());
		return R.ok();
	}


}
